package com.computergames.server.server.handler;

import com.computergames.server.domain.MatchingPlayer;
import com.computergames.server.message.LoginResponseMessage;
import com.computergames.server.message.MatchingRequestMessage;
import com.computergames.server.message.MatchingResponseMessage;
import com.computergames.server.service.Interface.UserService;
import com.computergames.server.service.matcher.MatchingManager;
import com.computergames.server.util.RedisUtil;
import com.computergames.server.util.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.TimeUnit;

public class MatchingRequestMessageHandler extends SimpleChannelInboundHandler<MatchingRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MatchingRequestMessage matchingRequestMessage) throws Exception {
//        UserService userService = SpringUtil.getBean(UserService.class);
//        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
//        int userId = userService.findUserId(matchingRequestMessage.getUsername());
//        //如何保证用户可以传输多个MatchingType，每个MatchingType对应的rating不一样
//        if(redisUtil.hasKey(String.valueOf(userId))){
//            MatchingPlayer player = null;
//            player.setUserId(userId);
//            player.setRating((int)redisUtil.hget(String.valueOf(userId), "rating"));
//            player.setMatchingType(matchingRequestMessage.getMatchingType());
//            MatchingManager.getInstance().addPlayerToQueue(player);
//        }else{
//            MatchingPlayer player = new MatchingPlayer(userId, 1000, matchingRequestMessage.getMatchingType());
//        }
//        MatchingManager.getInstance();
        channelHandlerContext.writeAndFlush(new MatchingResponseMessage(true, "正在进行匹配，请等待..."));

        channelHandlerContext.executor().schedule(() -> {
            // 发送第二条消息
            channelHandlerContext.writeAndFlush(new MatchingResponseMessage(true, "匹配成功，开始对局！"));
        }, 5, TimeUnit.SECONDS);
    }
}
